package org.example;

import org.apache.poi.xwpf.usermodel.*;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

public class WordTableReader {

    static class TableRowData {
        private int tableIndex;
        private int rowIndex;
        private List<String> cellValues;

        public TableRowData(int tableIndex, int rowIndex, List<String> cellValues) {
            this.tableIndex = tableIndex;
            this.rowIndex = rowIndex;
            this.cellValues = cellValues;
        }

        public int getTableIndex() {
            return tableIndex;
        }

        public List<String> getCellValues() {
            return cellValues;
        }
    }

    public static void main(String[] args) {
        String filePath = "D:/Desktop/6/1.docx";
        List<TableRowData> allTableData = new ArrayList<>();

        try (FileInputStream fis = new FileInputStream(filePath);
             XWPFDocument document = new XWPFDocument(fis)) {

            List<XWPFTable> tables = document.getTables();
            int tableIndex = 1;

            for (XWPFTable table : tables) {
                List<XWPFTableRow> rows = table.getRows();

                for (int rowIndex = 0; rowIndex < rows.size(); rowIndex++) {
                    XWPFTableRow row = rows.get(rowIndex);
                    List<XWPFTableCell> cells = row.getTableCells();

                    List<String> cellValues = new ArrayList<>();
                    boolean allEmpty = true;

                    for (XWPFTableCell cell : cells) {
                        String text = cell.getText().replace("\n", "").trim();
                        cellValues.add(text);
                        if (!text.isEmpty()) {
                            allEmpty = false;
                        }
                    }

                    if (allEmpty) continue;

                    TableRowData rowData = new TableRowData(tableIndex, rowIndex + 1, cellValues);
                    allTableData.add(rowData);
                }
                tableIndex++;
            }

            for (TableRowData rowData : allTableData) {
                if (rowData.getTableIndex() == 1 && rowData.getCellValues().size() >= 3) {
                    insert1GL(rowData.getCellValues());
                }
            }

            System.out.println("数据导入完成。");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void insert1GL(List<String> values) {
        try {
            Connection conn = DBHelper.getConnection();
            String sql = "INSERT INTO YWDB.1GL(FAMC, FALX, ZZR) VALUES (?, ?, ?)";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, values.get(0));
            pstmt.setString(2, values.get(1));
            pstmt.setString(3, values.get(2));
            pstmt.executeUpdate();
            pstmt.close();
        } catch (Exception e) {
            System.out.println("插入数据失败: " + values);
            e.printStackTrace();
        }
    }
}
